package by.home;

import java.util.ArrayList;

public class Stack {
    private Element top;

    public Element getTop() {
        return top;
    }

    public void setTop(Element top) {
        this.top = top;
    }
    public void push(String data){
        Element temp = new Element(data);
        if (top == null){
            top = temp;
        }else{
            temp.setPredElement(top);
            top.setNextElement(temp);
            top = temp;
        }
    }
    public String pop() {
        Element temp = top;
        if (top != null && top.getPredElement() != null) {
            Element pred = top.getPredElement();
            pred.setNextElement(null);
            top = pred;
        }else if (top == null){
            return null;
        }else {
            top = null;
        }
        return temp.getData();
    }
    public String poll(){
        if (top == null){
            return null;
        }
        return top.getData();
    }
    public ArrayList toArray(){
        Element temp = null;
        temp = top;
        ArrayList arrayList = new ArrayList();
        while (temp != null){
            arrayList.add(temp.getData());
            temp = temp.getPredElement();
        }
        return arrayList;
    }
}
